package com.aqie.easy.greedy;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 874 模拟机器人行走  todo
 */
public class RobotSim {
    /**
     * 1, 103ms
     * @param commands
     * @param obstacles
     * @return
     */
    public int robotSim(int[] commands, int[][] obstacles) {
        int max = 0;
        int[][] dx = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        int k = 0;
        Map<String, Boolean> map = new HashMap<>();
        for (int i = 0; i < obstacles.length; i++) {
            map.put(obstacles[i][0] + "," + obstacles[i][1], true);
        }
        int p = 0, q = 0;
        for (int command : commands) {
            if (command == -1) {
                k = (k + 1) % 4;
            } else if (command == -2) {
                k = (k + 4 - 1) % 4;
            } else {
                int cur[] = dx[k];
                for (int i = 0; i < command; i++) {
                    if (map.containsKey((p + cur[0]) + "," + (q + cur[1]))) {
                        break;
                    }
                    p += cur[0];
                    q += cur[1];
                }
                max = Math.max(max, p * p + q * q);
            }
        }
        return max;
    }


    /**
     * 2, 108ms
     * @param commands
     * @param obstacles
     * @return
     */
    public int robotSim2(int[] commands, int[][] obstacles) {
        Set<String> set = new HashSet<>();
        for (int[] obstacle : obstacles) {
            set.add(obstacle[0] + "," + obstacle[1]);
        }
        int x = 0;
        int y = 0;
        int flag = 1;
        int max = 0;
        for (int command : commands) {
            switch (command) {
                case -2:
                    if (flag == 1) {
                        flag = 4;
                    } else {
                        flag = flag - 1;
                    }
                    break;
                case -1:
                    if (flag == 4) {
                        flag = 1;
                    } else {
                        flag = flag + 1;
                    }
                    break;
                default:
                    if (flag == 1) {
                        for (int i = 0; i < command; i++) {
                            y = y + 1;
                            if (set.contains(x + "," + y)) {
                                y = y - 1;
                            }
                            max = Math.max(max, x * x + y * y);
                        }
                    } else if (flag == 2) {
                        for (int i = 0; i < command; i++) {
                            x = x + 1;
                            if (set.contains(x + "," + y)) {
                                x = x - 1;
                            }
                            max = Math.max(max, x * x + y * y);
                        }
                    } else if (flag == 3) {
                        for (int i = 0; i < command; i++) {
                            y = y - 1;
                            if (set.contains(x + "," + y)) {
                                y = y + 1;
                            }
                            max = Math.max(max, x * x + y * y);
                        }
                    } else if (flag == 4) {
                        for (int i = 0; i < command; i++) {
                            x = x - 1;
                            if (set.contains(x + "," + y)) {
                                x = x + 1;
                            }
                            max = Math.max(max, x * x + y * y);
                        }
                    }
            }
        }
        return max;
    }


}
